﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotDealD2.Models.ModelManager;
using HotDealD2.Models.ModelView;
using HotDealD2.Models;
using System.Web.Routing;
using System.IO;


namespace HotDealD2.Controllers
{
    [Authorize(Roles = "Business")]
    public class BusinessController : Controller
    {
        private DealModel dealModel;
        private AccountModel account;
        private PromotionModel proModel;
        private OrderDealModel orderModel;
        QLDataBaseDataContext dataContext;
        protected override void Initialize(RequestContext requestContext)
        {
            orderModel = new OrderDealModel();
            dealModel = new DealModel();
            proModel = new PromotionModel();
            dataContext = new QLDataBaseDataContext();
            account = new AccountModel();
            base.Initialize(requestContext);
        }
        //
        // GET: /Business/

        public ActionResult Index()
        {
            
            UpdateAccountView tv = account.GetAccountToUpdate(User.Identity.Name);
            var list = new SelectList(account.GetAllTinhThanh(), "MaTinhThanh", "TenTinhThanh", tv.SelectedCity);
            tv.CityList = list;
            list = new SelectList(account.GetAllQuanHuyen(1), "MaQuanHuyen", "TenQuanHuyen", tv.SelectedDistrict);
            tv.DistrictList = list;
            return View(tv);
        }

        [HttpPost]
        public ActionResult Index(UpdateAccountView regView)
        {
            regView.UserName = User.Identity.Name;
            if (ModelState.IsValid)
            {
                if (account.UpdateAccount(regView))
                {
                    return RedirectToAction("UpdateAccountSuccess", "Account");
                }
            }
            return View(regView);
        }
        public ActionResult PostedComment()
        {
            return View();
        }

        

        //
        // GET: /Business/RegisterPromotion
        public ActionResult RegisterPromotion()
        {
            RegisterDeal deal = new RegisterDeal();
            IList<LoaiHinhGiaoDich> listLoaiHinh = dealModel.GetAllLoaiHinhGiaoDich();
            IList<LoaiDeal> listLoaiDeal = dealModel.GetAllLoaiDeal();
            var list = new SelectList(listLoaiHinh, "MaLoaiHinhGiaoDich", "TenLoaiHinh", deal.MaLoaiHinhGiaoDich);
            deal.SelectLoaiHinhGiaoDich = list;
            list = new SelectList(listLoaiDeal, "MaLoaiDeal", "TenLoaiDeal", deal.MaLoaiDeal);
            deal.SelectLoaiDeal = list;

            //deal.NgayDang = DateTime.Today;
            //deal.NgayKetThuc = new DateTime(deal.NgayDang.Year, deal.NgayDang.Month, deal.NgayDang.Day + 5);
            //deal.ThoiHanSuDungVoucher = new DateTime(deal.NgayDang.Year, deal.NgayDang.Month + 1, deal.NgayDang.Day);
            //deal.ListImagesPath = new List<string>();
            return View("RegisterPromotion", deal);
            
        }
        //
        // POST: /Business/RegisterPromotion
        
        [HttpPost]
        public ActionResult RegisterPromotion(RegisterDeal deal, IEnumerable<HttpPostedFileBase> files)
        {
            if (ModelState.IsValid)
            {
                if (deal.NgayDang >= deal.NgayKetThuc || deal.NgayDang >= deal.ThoiHanSuDungVoucher || deal.NgayKetThuc >= deal.ThoiHanSuDungVoucher)
                {
                    return View("RegisterPromotion", deal);
                }

                List<string> listPath = new List<string>();
                foreach (var file in files)
                {
                    if (file != null)
                    {
                        if (file.ContentLength > 0)
                        {
                            var fileName = Path.GetFileName(file.FileName);
                            var path = Path.Combine(Server.MapPath("~/UploadImages"), fileName);
                            file.SaveAs(path);
                            listPath.Add(path.ToString());
                        }
                    }
                }
                deal.ListImagesPath = listPath;
                deal.UsernameDoanhNghiep = User.Identity.Name;
                dealModel.InsertDealChuaDuyet(deal);
                return RedirectToAction("ListPromotion");
            }
            IList<LoaiHinhGiaoDich> listLoaiHinh = dealModel.GetAllLoaiHinhGiaoDich();
            IList<LoaiDeal> listLoaiDeal = dealModel.GetAllLoaiDeal();
            var list = new SelectList(listLoaiHinh, "MaLoaiHinhGiaoDich", "TenLoaiHinh", deal.MaLoaiHinhGiaoDich);
            deal.SelectLoaiHinhGiaoDich = list;
            list = new SelectList(listLoaiDeal, "MaLoaiDeal", "TenLoaiDeal", deal.MaLoaiDeal);
            deal.SelectLoaiDeal = list;
            return View("RegisterPromotion", deal);
        }
        
        


        public ActionResult ListPromotion()
        {
            List<PromotionStatisticView> list = proModel.GetListPromotion(User.Identity.Name);
            return View(list);
        }

        public ActionResult ListOrdered(int id)
        {
            PromotionView pro = new PromotionView();
            pro.ProStatisticView = proModel.GetPromotion(User.Identity.Name, id);
            pro.OrderView = orderModel.GetListOrderPromotion(id);
            return View(pro);
        }

    }
}
